package com.reggie.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.reggie.constant.MessageConstant;
import com.reggie.constant.StatusConstant;
import com.reggie.dto.CategoryDTO;
import com.reggie.dto.CategoryPageQueryDTO;
import com.reggie.entity.Category;
import com.reggie.exception.DeletionNotAllowedException;
import com.reggie.mapper.CategoryMapper;
import com.reggie.mapper.DishMapper;
import com.reggie.mapper.SetmealMapper;
import com.reggie.properties.JwtProperties;
import com.reggie.result.PageResult;
import com.reggie.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 分页查询
     * @param pageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(CategoryPageQueryDTO pageQueryDTO) {

        //设置分页对象
        PageHelper.startPage(pageQueryDTO.getPage(),pageQueryDTO.getPageSize());
        //调用mapper实现查询
        Page<Category> page = categoryMapper.findPage(pageQueryDTO);
        //设置PageResult对象
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());
        return pageResult;
    }

    /**
     * 新增分类
     * @param categoryDTO
     */
    @Override
    public void save(CategoryDTO categoryDTO) {

        //属性拷贝
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);

        category.setStatus(StatusConstant.DISABLE);

        //调用mapper实现添加
        categoryMapper.add(category);
    }

    /**
     * 修改分类
     * @param categoryDTO
     */
    @Override
    public void update(CategoryDTO categoryDTO) {

        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);

        //调用mapper实现修改
        categoryMapper.update(category);
    }

    /**
     * 根据id删除分类
     * @param id
     */
    @Override
    public void deleteById(Long id) {

        //获取改分类下是否关联菜品
        Integer count = dishMapper.countById(id);
        //判断如果菜品关联，这抛出异常
        if (count > 0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        //获取该分类是和否关联套餐
        count = setmealMapper.countById(id);
        //判断如果套餐关联，这抛出异常
        if (count > 0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }

        //如果该菜品夏没有关联，则可以进行删除操作
        categoryMapper.deleteById(id);
    }

    /**
     * 启用禁用之间的修改
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        //调用mapper实现修改
        categoryMapper.startOrStop(status,id);
    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @Override
    public List<Category> findByType(Integer type) {
        List<Category> categoryList = categoryMapper.fingByType(type);
        return categoryList;
    }
}
